<%= content_for :javascript do %>
  <script src="/javascripts/app/documents.js"></script>
  <script src="/javascripts/app/documentReporteOrdinaria.js"></script>
  <script src="/javascripts/app/documentReporteExtraordinaria.js"></script>
  <script type="text/javascript">

    var mapCategory = {};
    <% @categories.each do |category| %>
        <% if category.category_type === "subheader" %>
            mapCategory[<%= category.id %>] = {<% category.subcategories.each_with_index do |subcategory,index| %> cat<%=index+1%>: <%= subcategory.id %> <%= subcategory != category.subcategories.last ? ',':'' %><% end %>};
        <% end %>
    <% end %>

   var addDocumentPopup = null;

    $(document).ready(function () {
      <% if @current_account.admin? %>
        $(".save-document").click(function (e) {
          e.preventDefault();
          $("#add-document-frame").contents().find("#new_document").submit();
        });
        
        $(".save-document-permissions").click(function (e) {
          e.preventDefault();
          $("#view-document-permissions-frame").contents().find(".form-horizontal").submit();
        });          
      <% end %>

      <% if @has_active_categories %>
        $(".nav-list a").click(function (e) {
          e.preventDefault();
          $(".nav-list li").removeClass("active");
          $(this).parent().addClass("active");
          
          var categoryType = $(this).attr("data-type");
          var categoryId = parseInt($(this).attr("data-id"));
          
          if (categoryType == "subheader") {
            $("#otras").hide();
            $("#reportes-comision").show();
            $(".search-ordinaria").attr("data-id",mapCategory[categoryId].cat1);
            $(".search-extraordinaria").attr("data-id",mapCategory[categoryId].cat2);
            App.OrdinariaDocumentController = new OrdinariaDocumentController({"category": mapCategory[categoryId].cat1});
            App.ExtraOrdinariaDocumentController = new ExtraOrdinariaDocumentController({"category": mapCategory[categoryId].cat2});
          } else {
            $("#otras").show();
            $("#reportes-comision").hide();
            $(".search-general").attr("data-id",categoryId);
            App.DocumentController = new DocumentController({"category": categoryId});
          }
        });
        <% if @active_category.category_type == "subheader" %>
          App.OrdinariaDocumentController = new OrdinariaDocumentController({"category": <%= @active_subcategory_ordinaria.id %>});
          $(".search-ordinaria").attr("data-id",<%= @active_subcategory_ordinaria.id %>);
          
          App.ExtraOrdinariaDocumentController = new ExtraOrdinariaDocumentController({"category": <%= @active_subcategory_extraordinaria.id %>});          
          $(".search-extraordinaria").attr("data-id",<%= @active_subcategory_extraordinaria.id %>);

        <% else %>
          App.DocumentController = new DocumentController({"category": <%= @active_category.id %>});
          $(".search-general").attr("data-id",<%= @active_category.id %>);          
        <% end %>
          
        $(".search-extraordinaria").keypress(function(e) {
            var code = (e.keyCode ? e.keyCode : e.which);
            if (code == 13) {
              App.ExtraOrdinariaDocumentController.search({
                "category": $(this).attr("data-id"),
                "search_query": $(this).val()
              });
            }
        });          
          
        $(".search-ordinaria").keypress(function(e) {
            var code = (e.keyCode ? e.keyCode : e.which);
            if (code == 13) {
              App.OrdinariaDocumentController.search({
                "category": $(this).attr("data-id"),
                "search_query": $(this).val()
              });
            }
        });          
          
        $(".search-general").keypress(function(e) {
             var code = (e.keyCode ? e.keyCode : e.which);
             if (code == 13) {
               App.DocumentController.search({
                 "category": $(this).attr("data-id"),
                 "search_query": $(this).val()
               });
             }
         });          
          
      <% end %>
    });

      function addDocument(categoryId) {
        $("#add-document-frame").contents().find("#document_category_id").val(categoryId);
        addDocumentPopup = $("#add-document").modal("show");
      }
      
      function deleteDocument(documentId,categoryId) {
        if (<% @ordinarias.each_with_index do |category,index| %> categoryId == <%= category.id %> <%= category.id != @ordinarias.last.id ? '||':'' %> <% end %>) {
          App.OrdinariaDocumentController.deleteDocument(documentId);
        } else if (<% @extraordinarias.each_with_index do |category,index| %> categoryId == <%= category.id %> <%= category.id != @extraordinarias.last.id ? '||':'' %> <% end %>) {
          App.ExtraOrdinariaDocumentController.deleteDocument(documentId);
        } else {
          App.DocumentController.deleteDocument(documentId);
        }
      }
      
      
      function viewPermissions(documentId,categoryId) {
          src = "/documents/permissions/"+documentId;
          $("#view-document-permissions-frame").attr("src",src);
          viewPermissionsPopup = $("#view-document-permissions").modal("show");
      }      

  </script>
<% end %>
    <div class="page-header">
      <h1>Reportes de Monitoreo</h1>
    </div>
    <div class="row">
        <% if @has_active_categories %>
              <div class="span3">
                  <div class="well" style="padding: 8px 0;">
                    <ul class="nav nav-list">
                      <% @categories.each do |category| %>                
                          <li class="<%=  @active_category_id == category.id ? 'active' : ''%> <%= category.category_type == 'title' || category.category_type == 'standard-title' ? 'nav-header' : '' %>">
                            <% if category.category_type == 'standard' || category.category_type == 'subheader' || category.category_type == 'standard-title' %>
                              <a href="#" data-id="<%= category.id %>" data-type="<%= category.category_type %>" style="color: #<%= category.category_type == 'standard-title' ? '999999;' : '' %>" ><%= category.name %></a>
                            <% else %>
                              <%= category.name %>
                            <% end %>
                          </li>
                          <li class="divider"></li>                
                      <% end %>
                    </ul>
                  </div>
              </div>
              <div class="span9 <%= @active_category.category_type == "subheader" ? "" : "hide" %>" id="reportes-comision">
                <ul class="nav nav-tabs" id="myTab">
                  <li class="active"><a href="#ordinaria" data-toggle="tab" class="sesion-ordinaria">Sesión Ordinaria</a></li>
                  <li><a href="#extraordinaria" data-toggle="tab" class="sesion-extraordinaria">Sesión Extraordinaria</a></li>
                </ul>
                <div class="tab-content">
                  <div class="tab-pane active" id="ordinaria">
                    <div class="pull-right input-append" style="padding-top:30px;">
                      <input type="text" placeholder="Buscar Informes por nombre..." class="input-xlarge search-query search-ordinaria">          
                      </input>
                    </div>
                    <div class="clearfix"></div>
                    <div id="ordinaria-section-container">
                    </div>
                    <div class="ordinaria-pagination pagination pagination-centered">    
                    </div>
                  </div>
                  <div class="tab-pane" id="extraordinaria">
                    <div class="pull-right input-append" style="padding-top:30px;">
                      <input type="text" placeholder="Buscar Informes por nombre..." class="input-xlarge search-query search-extraordinaria">
                      </input>
                    </div>
                    <div class="clearfix"></div>            
                    <div id="extraordinaria-section-container">
                    </div>
                    <div class="extraordinaria-pagination pagination pagination-centered">    
                    </div>
                  </div>
                </div>
              </div>
              <div class="span9 <%= @active_category.category_type == "subheader" ? "hide" : "" %>" id="otras">
                    <div class="pull-right input-append" style="padding-top:30px;">
                      <input type="text" placeholder="Buscar Informes por nombre..." class="input-xlarge search-query search-general">
                      </input>
                    </div>
                    <div class="clearfix"></div>        
                    <div id="section-container"></div>
                    <div class="documents-pagination pagination pagination-centered"></div>
                  </div>
              </div>
        <% end %>

<%= content_for :modal do %>
  <% if @current_account.admin? %>
    <div class="modal hide" id="add-document" style="height:320px;">
        <div>
          <!-- ** widget header ** -->
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h4>Agregar documento</h4>
          </div>
          <!-- ** ./ widget header ** -->
          <!-- ** dummy content ** -->
          <iframe id="add-document-frame" src="/documents/new" style="width:100%;border:0px;height:220px;"></iframe>
          <!-- ** ./ dummy content ** -->
        </div>
        <div class="modal-footer">
          <a href="#" class="btn btn-primary save-document">Cargar documento</a> <a href="#" class="btn btn-warning close-owner" data-dismiss="modal">Cerrar</a>
        </div>
    </div>
    <div class="modal hide" id="view-document-permissions" style="height:320px;">
      <div>
        <!-- ** widget header ** -->
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
          <h4>Permisos del archivo</h4>
        </div>
        <!-- ** ./ widget header ** -->
        <!-- ** dummy content ** -->
        <iframe id="view-document-permissions-frame" src="" style="width:100%;border:0px;height:220px;"></iframe>
        <!-- ** ./ dummy content ** -->
      </div>
      <div class="modal-footer">
        <a href="#" class="btn btn-primary save-document-permissions">Grabar permisos</a> <a href="#" class="btn btn-warning close-document-permissions" data-dismiss="modal">Cerrar</a>
      </div>
    </div>  
  <% end %>
<% end %>
  
<%= content_for :templates do %>
    <script id="documents-template" type="text/x-handlebars-template">
      <section id="{{category_code}}" style="padding-top: 0px;">
        <h3>
          <span>{{category_name}}</span>
          <div class="pull-right">
            <% if @current_account.admin? %>
              <a href="#" class="btn btn-primary btn-small add-document" onclick="addDocument({{category_id}})">Añadir documento</a>
            <% end %>
          </div>
        </h3>      
        <div class="clearfix">
            {{#if documents}}
            <table class="table table-bordered table-hover">
              <tr>
                <th>Fecha</th>
                <th>Nombre</th>
                <th>Descripción</th>
                <th>Acciones</th>
              </tr>
              {{#each_with_index documents}}
                {{#with this}}
                <tr>
                  <td>{{#formatDate upload_at}}
                        j f Y
                      {{/formatDate}}
                  </td>
                  <td>{{name}}</td>
                  <td>{{description}}</td>
                  <td>
                    <a href="{{content_url}}">Descargar</a>
                    <% if @current_account.admin? %>
                      | <a href="#" onclick="deleteDocument({{id}},{{category_id}})">Eliminar</a>                      
                      | <a href="#" onclick="viewPermissions({{id}})">Permisos</a>                      
                    <% end %>
                  </td>
                </tr>
              {{/with}}
            {{/each_with_index}}                          
            </table>
            {{/if}}
            {{#unless documents}}No hay documentos que mostrar aun.{{/unless}}
        </div>
      </section>    
    </script>   
    <script id="pagination" type="text/x-handlebars-template">
      <ul>{{{create_pagination_links currentPage perPage totalEntries paginateFunction category_id}}}</ul>
    </script>    
<% end %>
